﻿
using SYJ.Model.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using Dapper;
using SYJ.IDAL;
using SYJ.Factory;
using SYJ.Model;

namespace SYJ.MySqlDAL
{
    public class ContestResultDAL : BaseDal<ContestResult>, IContestResultDAL 
    {

        /// <summary>
        /// 多表查询分页获取
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<ContestResult> GetList(QueryModel query, out long count)
        {

            List<ContestResult> list = null;
            using (Conn)
            {
                string sqlCount = string.Format("SELECT Count(*) FROM notice a LEFT JOIN notice_type b on b.id=a.type LEFT JOIN sys_user c on c.id=a.user_id where 1=1 {0} ", query.SqlWhere);
                string sql = string.Format(@"SELECT a.id,a.title,a.user_id,a.create_time,a.type,a.`status`,b.title as notice_type,c.author FROM notice a
LEFT JOIN notice_type b on b.id=a.type
LEFT JOIN sys_user c on c.id=a.user_id where 1=1 {2} order by {3} {4} LIMIT {0},{1};", query.PageSize * (query.PageIndex - 1), query.PageSize, query.SqlWhere, query.OrderName, query.SortOrder);
                if (query.Param == null)
                {
                    list = Conn.Query<ContestResult>(sql).AsList();
                    count = Conn.ExecuteScalar<long>(sqlCount);
                }
                else
                {
                    list = Conn.Query<ContestResult>(sql, query.Param).AsList();
                    count = Conn.ExecuteScalar<long>(sqlCount, query.Param);
                }
            }
            return list;
        }
   }
}
